package com.star.common.core.utils;

import cn.hutool.core.text.CharSequenceUtil;
import com.star.common.core.contract.enums.IEnum;
import com.star.common.core.contract.enums.ResultCodeEnum;
import com.star.common.core.contract.exception.ServiceErrorException;
import com.star.common.core.contract.exception.ServiceException;

/**
 * 业务断言类，断言不通过将抛出ServiceException
 *
 * @author zhouhengzhe
 * @公众号 小白的Java进阶之路
 */
public class BizAssert {

    /**
     * 私有构造方法
     */
    private BizAssert() {
    }

    public static void isTrueWarn(Boolean expression, Integer code, String errorMsg, Object... params) {
        if (Boolean.FALSE.equals(expression)) {
            throw new ServiceException(code, CharSequenceUtil.format(errorMsg, params));
        }
    }

    public static void isTrueWarn(Boolean expression, String errorMsg, Object... params) {
        if (Boolean.FALSE.equals(expression)) {
            throw new ServiceException(ResultCodeEnum.FAIL.getCode(), CharSequenceUtil.format(errorMsg, params));
        }
    }

    public static void isTrueWarn(Boolean expression, IEnum<Integer> iEnum) {
        if (Boolean.FALSE.equals(expression)) {
            throw new ServiceException(iEnum);
        }
    }

    public static void isTrueError(Boolean expression, IEnum<Integer> iEnum) {
        if (Boolean.FALSE.equals(expression)) {
            throw new ServiceErrorException(iEnum);
        }
    }


    public static void isTrueWarn(Boolean expression, IEnum<Integer> iEnum, String[] params) {
        if (Boolean.FALSE.equals(expression)) {
            throw new ServiceException(iEnum, params);
        }
    }


    public static void isFalseWarn(Boolean expression, Integer code, String errorMsg, Object... params) {
        if (Boolean.TRUE.equals(expression)) {
            throw new ServiceException(code, CharSequenceUtil.format(errorMsg, params));
        }
    }

    public static void isFalseWarn(Boolean expression, IEnum<Integer> iEnum) {
        if (Boolean.TRUE.equals(expression)) {
            throw new ServiceException(iEnum);
        }
    }

    public static void isFalseError(Boolean expression, IEnum<Integer> iEnum) {
        if (Boolean.TRUE.equals(expression)) {
            throw new ServiceErrorException(iEnum);
        }
    }

    public static <T> void notNullWarn(T object, String errorMsg, Object... params) {
        if (object == null) {
            throw new ServiceException(ResultCodeEnum.FAIL.getCode(), CharSequenceUtil.format(errorMsg, params));
        }
    }


    public static <T> void notNullWarn(T object, IEnum<Integer> iEnum) {
        if (object == null) {
            throw new ServiceException(iEnum);
        }
    }

    public static <T> void notNullError(T object, IEnum<Integer> iEnum) {
        if (object == null) {
            throw new ServiceErrorException(iEnum);
        }
    }

    /**
     * 想要object为空，但是确不为空，报错
     *
     * @param object 对象
     * @param iEnum  错误枚举
     * @param <T>    对象类型
     */
    public static <T> void isNullWarn(T object, IEnum<Integer> iEnum) {
        if (object != null) {
            throw new ServiceException(iEnum);
        }
    }

    public static <T> void notNullWarn(T object) {
        if (object == null) {
            throw new ServiceException(ResultCodeEnum.ARGUMENT_NULL);
        }
    }

    public static void notBlankWarn(String msg, Integer code, String errorMsg, Object... params) {
        if (msg == null || msg.isEmpty()) {
            throw new ServiceException(code, CharSequenceUtil.format(errorMsg, params));
        }
    }

    public static void notBlankWarn(String string, IEnum<Integer> iEnum) {
        if (string == null || string.isEmpty()) {
            throw new ServiceException(iEnum);
        }
    }

    public static void notBlankError(String string, IEnum<Integer> iEnum) {
        if (string == null || string.isEmpty()) {
            throw new ServiceErrorException(iEnum);
        }
    }

    public static void notBlankWarn(String string) {
        if (string == null || string.isEmpty()) {
            throw new ServiceException(ResultCodeEnum.ARGUMENT_NULL);
        }
    }

    public static <E, T extends Iterable<E>> T notEmptyWarn(T iterable,IEnum iEnum) {
        if (iterable == null || !iterable.iterator().hasNext()) {
            throw new ServiceException(iEnum);
        }
        return iterable;
    }

    /**
     * 判断集合不为空，为空报错，而且报错的信息会被Cat捕获展示出来
     *
     * @param iterable 集合
     * @param iEnum    错误枚举
     * @param <E>      集合元素类型
     * @param <T>      集合类型
     * @return 集合
     */
    public static <E, T extends Iterable<E>> T notEmptyError(T iterable, IEnum<Integer> iEnum, String[] params) {
        if (iterable == null || !iterable.iterator().hasNext()) {
            throw new ServiceErrorException(iEnum, params);
        }
        return iterable;
    }

    public static <E, T extends Iterable<E>> T notEmptyWarn(T iterable) {
        if (iterable == null || !iterable.iterator().hasNext()) {
            throw new ServiceException(ResultCodeEnum.ARGUMENT_NULL);
        }
        return iterable;
    }
}
